<?php

class GalactusViewSchedule extends GalactusViewCalendar {

	function __construct($control) {
		GalactusView::__construct($control);
	}

	public function retrieveCSS( ) {
		return '
			.table-schedule th {
				text-align: center !important;
			}
		';
	}

	public function retrieveJS( ) {
		return '
			function galactus_view_schedule_show(calendar, schedule) {
				var MAXSLOTS = 3;
				var amount_slots = 0;
				var colors = {
					available: "rgb(245, 245, 245)",
					forbidden: "rgb(242, 222, 222)",
					used: "rgb(217, 237, 247)"
				};
				var user_id = "'.$this->control->user_id.'";

				$(".table td").each(function( ) {
					var date_current = $(this).find("input:hidden").val( );
					$(this).css("background-color", colors.forbidden);
					for(var i = 0; i < calendar.length; ++i) {
						if(date_current >= calendar[i].date_start && date_current <= calendar[i].date_end)
							$(this).css("background-color", colors.available);
					}
					if(schedule != undefined) {
						for(var i = 0; i < schedule.length; ++i) {
							if(date_current == schedule[i].date_start && user_id != schedule[i].user_id) {
								$(this).css("background-color", colors.forbidden);
							}
							else if(date_current == schedule[i].date_start && user_id == schedule[i].user_id) {
								$(this).css("background-color", colors.used);
								$(this).find("input:hidden").addClass("params");
								++amount_slots;
							}
						}
					}
				});

				$(".table td").hover(function( ) {
					if($(this).css("background-color") != colors.forbidden)
						$(this).css("cursor", "pointer");
				});

				$(".table td").click(function( ) {
					if($(this).css("background-color") == colors.available && amount_slots < MAXSLOTS) {
						$(this).css("background-color", colors.used);
						$(this).find("input:hidden").addClass("params");
						++amount_slots;
					}
					else if($(this).css("background-color") == colors.used) {
						$(this).css("background-color", colors.available);
						$(this).find("input:hidden").removeClass("params");
						--amount_slots;
					}
				});


				$("#button-clear").click(function( ) {
					$(".table td").each(function( ) {
						if($(this).css("background-color") == colors.used) {
							$(this).css("background-color", colors.available);
							--amount_slots;
						}
					});
				});

				$("#button-confirm").click(function( ) {
					$("#contentarea").load(
						"'.URL.'SCHEDULE/SCHEDULE",
						$(".params").serializeArray( )
					);
				});
			}				
		';
	}

	public function show($telescope_id) {
		$telescope = reset($this->control->model->retrieveTelescope($telescope_id));
		$calendar = $this->control->model->retrieveCalendar($telescope_id);
		$schedule = '<div class="span12"><p class="lead text-center">Calendário de observações não definido!</p></div>';
		$json_calendar = "undefined";
		if($calendar != NULL) {
			$date_start = reset($calendar);
			$date_start = new DateTime($date_start["date_start"]);
			$date_end = end($calendar);
			$date_end = new DateTime($date_end["date_end"]);
			$date_diff = $date_end->diff($date_start);
			$timeslots = $this->retrieveTimeslots($date_start, $date_diff->d+1);
			$schedule = '<div class="span6">'.$this->retrieveSchedule($timeslots, 0, 0, $timeslots['times']/2).'</div>';
			$schedule .= '<div class="span6">'.$this->retrieveSchedule($timeslots, 1, $timeslots['times']/2, $timeslots['times']).'</div>';
			$json_calendar = json_encode($calendar);
			$json_schedule = $this->control->model->retrieveSchedule($telescope_id);
			$json_schedule = !empty($json_schedule)? json_encode($json_schedule) : "undefined";
		}
		echo '
			<input type="hidden" class="params" name="params[ ]" id="hidden-telescope-id" value="'.$telescope["telescope_id"].'" />
			<div class="container-fluid">
				<div class="row-fluid">
					<div class="alert alert-info fade in span12">
						<button type="button" class="close" data-dismiss="alert">×</button>
						Você escolheu o telescópio <strong>'.$telescope["name"].'</strong>!
					</div>
				</div>
				<div class="row-fluid">'.$schedule.'</div>
				<div class="row-fluid">
					<div class="span6">
						<span>Legenda de Horários: </span>                      
						<span class="label">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vago&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>
						<span class="label label-important">&nbsp;&nbsp;proibido&nbsp;&nbsp;</span>
						<span class="label label-info">agendado</span>
					</div>
					<button type="button" class="btn btn-small offset2 span2" id="button-clear">Limpar</button>
					<button type="button" class="btn btn-primary btn-small span2" id="button-confirm">Confirmar</button>
				</div>
			</div>
			<script type="text/javascript">$(galactus_view_schedule_show('.$json_calendar.','.$json_schedule.'));</script>
		';
	}

	private function retrieveSchedule($timeslots, $header_start, $row_start, $row_end) {
		$en = array('Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat');
		$pt = array('Dom', 'Seg', 'Ter', 'Qua', 'Qui', 'Sex', 'Sáb');
		$thead = '';
		for($i = $header_start; $i < $timeslots['days']; ++$i) {
			$thead .= '<th>'.str_replace(' ', '<br />', str_replace($en, $pt, $timeslots['slots'][$timeslots['times']*$i]->format('D d/m'))).'</th>';
		}
		if($header_start) {
			$thead .= '<th>'.str_replace(' ', '<br />', str_replace($en, $pt, end($timeslots['slots'])->format('D d/m'))).'</th>';
		}
		$tbody = '';
		for($i = $row_start; $i < $row_end; ++$i) {
			$tbody .= '<tr>';
			$tbody .= '<th>'.$timeslots['slots'][$i]->format('H:i').'</th>';
			for($j = 0; $j < $timeslots['days']; ++$j)
				$tbody .= '<td><input type="hidden" name="params[ ]" value="'.$timeslots['slots'][$j*$timeslots['times']+$i]->format('Y-m-d H:i:s').'" /></td>';
			$tbody .= '</tr>';
		}
		return '
			<table class="schedule table table-bordered">
				<thead>
					<tr>
						<th></th>'.$thead.'
					</tr>
				</thead>
				<tbody>'.$tbody.'</tbody>
			</table>
		';
	}

}
?>
